package com.oracle.third.service.email.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.third.service.email.api.EmailServiceApi;
import com.oracle.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;

@Service
public class EmailServiceImpl implements EmailServiceApi {

    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${cache_key_prefix}")
    private String cachePrefix;


    @Override
    public ResultEntity checkedRandomCode(String email, String requestCode) {
        //获取key
        String cacheKey = this.cachePrefix+"email:"+email;
        Object obj = this.redisTemplate.opsForValue().get(cacheKey);
        if (obj==null){
            return new ResultEntity(ResultEntity.CODE_FAIL,"验证码不存在或已失效");
        }
        return new ResultEntity(ResultEntity.CODE_SUCCESS,"success");
    }

    @Override
    public ResultEntity checkedCanReSndRandomCode(String email) {
        //获取Key
        String reSendKey = this.cachePrefix+"reSendEmail:"+email;
        Object obj = this.redisTemplate.opsForValue().get(reSendKey);
        if (obj!=null){
            return new ResultEntity(ResultEntity.CODE_FAIL,"暂时无法发送验证码");
        }
        return new ResultEntity(ResultEntity.CODE_SUCCESS,"success");
    }
}
